package com.example.score.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.score.entity.Teacher;
import com.example.score.service.TeacherService;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;
/**
 * @author shenrui
 * @date 2021/9/29
 * @description 对老师增删改查等操作
 */
@Controller
public class TeacherController {

    @Autowired
    TeacherService teacherService;
    /**
     *@description 查询教师列表
     * @return String
     */
    @GetMapping("/admin_teacher_list")
    public String TeacherManage(HttpSession session) {
        List<Teacher> teacherList = teacherService.list();
        session.setAttribute("teacherList", teacherList);
        return "admin_teacher_list";
    }

    /**
     *@description 跳转教师信息修改页面
     * @return String
     */
    @GetMapping("/teacher_revise_jump/{id}")
    public String reviceTeacherJump(@PathVariable("id") Long id,
                                    HttpSession session) {
        Teacher teacher = teacherService.getById(id);
        session.setAttribute("teacherInfo",teacher);
        return "redirect:/revise_teacher_info";
    }

    /**
     *@description 修改教师信息
     * @return String
     */
    @PostMapping("/submit_teacher_info")
    public String reviceStudentInfo(Teacher teacher, Model model) {
        //填入信息不能为空
        if (StringUtils.isNullOrEmpty(teacher.getTeacherName())) {
            model.addAttribute("teacherMsg", "信息不能为空");
            return "revise_teacher_info";

        }
        //进行更新
        Boolean bool = teacherService.updateById(teacher);
        if (bool) {
            return "redirect:/admin_teacher_list";
        } else {
            model.addAttribute("stuMsg", "修改失败");
            return "revise_teacher_info";
        }

    }
    /**
     *@description 跳转密码修改页面
     * @return String
     */
    @GetMapping("/teacher_password_jump/{id}")
    public String teacherPasswordJump(@PathVariable("id") Long id,
                                      HttpSession session) {
        Teacher teacher = teacherService.getById(id);
        session.setAttribute("teacherInfo",teacher);
        return "redirect:/revise_teacher_password";
    }
    /**
     *@description 修改密码
     * @return String
     */
    @PostMapping("/submit_teacher_password")
    public String reviceTeacherPassword(Teacher teacher, Model model) {
        //填入信息不能为空
        if (StringUtils.isNullOrEmpty(teacher.getTeacherPassword())) {
            model.addAttribute("teacherMsg", "密码不能为空");
            return "revise_teacher_password";

        }
        BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder();
        String hashPass = bcryptPasswordEncoder.encode(teacher.getTeacherPassword());
        teacher.setTeacherPassword(hashPass);
        //进行更新
        Boolean bool = teacherService.updateById(teacher);
        if (bool) {
            return "redirect:/admin_teacher_list";
        } else {
            model.addAttribute("teacherMsg", "修改失败");
            return "revise_teacher_password";
        }

    }

    /**
     *@description 改变教师账号状态
     * @return String
     */
    @RequestMapping("/teacher_status/{id}")
    public String teacherStatus(@PathVariable("id") Long id) {
        Teacher teacher = teacherService.getById(id);
        if(teacher.getTeacherStatus() == 1) {
            teacher.setTeacherStatus(0);
        } else {
            teacher.setTeacherStatus(1);
        }
        teacherService.updateById(teacher);
        return "redirect:/admin_teacher_list";
    }

    /**
     *@description 添加教师
     * @return String
     */
    @PostMapping("/submit_teacher_add")
    public String studentAdd(Teacher teacher, Model model) {
        //填入信息不能为空
        if (StringUtils.isNullOrEmpty(teacher.getTeacherAccount()) || StringUtils.isNullOrEmpty(teacher.getTeacherPassword()) || StringUtils.isNullOrEmpty(teacher.getTeacherName()) ) {
            model.addAttribute("stuMsg", "信息不能为空");
            return "admin_stu_add";

        }
        //用户名不能重复
        if (teacherService.count(new LambdaQueryWrapper<Teacher>().eq(Teacher::getTeacherAccount, teacher.getTeacherAccount())) > 0) {
            model.addAttribute("teacherMsg", "学号重复");
            return "admin_teacher_add";
        }
        BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder();
        String hashPass = bcryptPasswordEncoder.encode(teacher.getTeacherPassword());
        teacher.setTeacherPassword(hashPass);
        //判断是否成功插入数据库
        if(teacherService.save(teacher)) {
            return "redirect:/admin_teacher_list";
        } else {
            model.addAttribute("teacherMsg", "添加失败");
            return "admin_teacher_add";
        }
    }
}
